<template>
    <div>
        <div class="nav">
            <div @click="back" class="icon"><van-icon name="arrow-left" /></div>
            <div class="add">收货地址</div>
            <div>&nbsp;</div>
        </div>
        <div class="address-list">
            <div class="address-item" v-for="a in addressList" :key="a.addressId">
                <div class="add-con">
                    <div class="add-name">{{ a.contactInfo.name }} <span>{{ a.contactInfo.tel }}</span> <i v-show="a.contactInfo.isDefault">默认</i></div>
                    <div>{{ a.contactInfo.province }} {{ a.contactInfo.city }} {{ a.contactInfo.county }} {{ a.contactInfo.addressDetail }} ({{ a.contactInfo.areaCode }})</div>
                </div>
                <div class="add-pot">
                    <div class="add-change" @click="selAddress(a.addressId)">选择</div>
                    <div class="add-del" @click="delAddress(a.addressId)">删除</div>
                </div>
            </div>
        </div>
        <van-button @click="ToAddaddress" style="position: fixed; bottom: 15px; width: 95%; margin-left: 2.5%;background-color: #ff6700;color: aliceblue;" size="large" round block >+新建收货地址</van-button>
        
    </div>
</template>

<script>
export default {
    data(){
        return{
            addressList:[]
        }
    },

    methods:{
        back(){
            this.$router.go(-1)
        },
        ToAddaddress(){
            this.$router.push('/addaddress')
        },

        showAddress(){
            this.addressList = JSON.parse(localStorage.addressList || '[]')
        },
        delAddress(id){
            this.addressList = this.addressList.filter(k => k.addressId != id)
            let addressLists = JSON.parse(localStorage.addressList || '[]')
            addressLists = [...addressLists.filter(k => k.addressId != id)]
            localStorage.addressList = JSON.stringify(addressLists)
        },
        selAddress(id){
            let addressList = JSON.parse(localStorage.addressList || '[]')
            let defaultAddress = addressList.filter(k => k.isSelect)[0]

            if(defaultAddress){
                let address = {
                    addressId: defaultAddress.addressId,
                    contactInfo: defaultAddress.contactInfo,
                    isSelect: false
                }
                addressList = [address, ...addressList.filter(k => k.addressId != defaultAddress.addressId)]
                localStorage.addressList = JSON.stringify(addressList)
            }
            let selAddress = addressList.filter(k => k.addressId == id)[0]
            let selAdd = {
            addressId: id,
            contactInfo: selAddress.contactInfo,
            isSelect: true
            }
            addressList = [selAdd, ...addressList.filter(k => k.addressId != id)]
            localStorage.addressList = JSON.stringify(addressList)

            this.$router.go(-1)
        }
    },
    mounted(){
        this.showAddress()
    }
}
</script>

<style lang="scss" scoped>
.nav{
    height: 12vw;
    display: flex;
    justify-content: space-between;

    .icon{
        margin-top: 3.5vw;
        margin-left: 10px;
    }
    .add{
        line-height: 12vw;
        margin-right:15px;
    }
   
}

.address-list{
    width: 100vw;
    height: 90vh;
    padding: 2vw 3vw;
    box-sizing: border-box;
    background-color: #f2f2f2;

    .address-item{
        padding: 2vw 3vw;
        background-color: #ffffff;
        border-radius: 2vw;
        margin-bottom: 4vw;
        
        
        .add-con{
            color: #333333;
            border-bottom: #ececec 1px dotted;
            padding-bottom: 4vw;
            
            .add-name{
                line-height: 7vw;
                color: #000;
                font-size: 22px;

                span{
                    font-size: 12px;
                }
                
                i{
                    display: inline-block;
                    line-height: 4.5vw;
                    font-size: 13px;
                    color: #ff6700;
                    font-style: normal;
                    padding: 0 1vw;
                    border: #ff6700 1.5px solid;
                    border-radius: 1vw;
                }
            }
        }
        
        .add-pot{
            width: 100%;
            height: 10vw;
            line-height: 10vw;
            text-align: center;
            display: flex;
            justify-content: space-between;

            .add-change{
                width: 50%;
            }

            .add-del{
                width: 50%;
                color: red;
            }
        }
    }
}
</style>